package web.mb;

import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import metier.entities.Annonceur;
import metier.entities.Publicite;
import metier.sess.IAnnonceurLocal;
import metier.sess.IPubliciteLocal;


@ManagedBean
@RequestScoped
public class PubMB {
	
	@EJB
	private IPubliciteLocal metier;
	
	@EJB
	private IAnnonceurLocal metiera;
	
	private Integer Id;
	private Publicite publicite=new Publicite();
	private Publicite pub=new Publicite();
	private Integer IdAnn;
	private Integer edit=new Integer(0);
	
	
	public Integer getId() {
		return Id;
	}
	
	public void setId(Integer id) {
		Id = id;
	}
	

	public Publicite getPublicite() {
		return publicite;
	}

	public void setPublicite(Publicite publicite) {
		this.publicite = publicite;
	}

	public Integer getIdAnn() {
		return IdAnn;
	}

	public void setIdAnn(Integer idAnn) {
		IdAnn = idAnn;
	}

	
	
	public Publicite getPub() {
		
		return pub;
	}

	public void setPub(Publicite pub) {
		this.pub = pub;
	}
	

	public Integer getEdit() {
		return edit;
	}

	public void setEdit(Integer edit) {
		System.out.println(edit);
		this.edit = edit;
	}

	public String Ajouter()
	{
		publicite.setAnnonceur(metiera.getAnnonceur(IdAnn));
		publicite.setRest(publicite.getNbrApparition());
		metier.addPublicite(publicite);
		
		return "ManageAdvertisements.xhtml";
	}
	
	
	public String edit(Integer Id)
	{
		this.Id=Id;
		Publicite p=metier.getPublicite(Id);
		this.publicite.setLien(p.getImage());
		this.publicite.setLien(p.getLien());
		this.publicite.setNbrApparition(p.getNbrApparition());
		this.publicite.setRest(p.getRest());
    	IdAnn=p.getAnnonceur().getIdAnnonceur();
		return "EditAdvertisements.xhtml";
		
	}
	
	public String Modifier()
	{
		
		Publicite p=metier.getPublicite(Id);
		p.setLien(publicite.getImage());
		p.setLien(publicite.getLien());
		p.setNbrApparition(publicite.getNbrApparition());
		p.setRest(publicite.getRest());
		p.setAnnonceur(metiera.getAnnonceur(IdAnn));
		metier.updatePublicite(p);
		return "ManageAdvertisements.xhtml";
	}

	
	public String Supprimer(Integer Id)
	{
		
		metier.deletePublicite(Id);
		return "succes";
	}
	
	
	public List<Publicite> getListPublicite()
	{
		return metier.getAllPublicites();
	}

	
	
	public List<Annonceur> getListAnnonceurs()
	{
		return metiera.getAllAnnonceursActive();
	}

}
